﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RookieLibSharp.Ui
{
    public partial class TextBoxWithDataPick : UserControl
    {
        public TextBoxWithDataPick()
        {
            InitializeComponent();
        }
        public string text
        {
            get { return this.textBox.Text; }
            set { this.textBox.Text = value; }
        }
        public DataTable dataSource = null;
        public string columnName = "";
        private void TextBoxWithDataPick_Load(object sender, EventArgs e)
        {
            this.Height = this.textBox.Height;
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox.Text == "")
            {
                this.Height = this.textBox.Height;
                this.listBox.Visible = false;
                this.SendToBack();
                return;
            }
            //这些情况下不弹出选择框
            if ((dataSource==null) || (columnName=="")||(!dataSource.Columns.Contains(columnName)))
            {
                return;
            }
            //根据用户输入的内容，筛选出于内容相符的记录并显示在listbox中
            this.listBox.Items.Clear();
            for (int idx = 0; idx < dataSource.Rows.Count;idx++ )
            {
                if (dataSource.Rows[idx][columnName].ToString().IndexOf(this.textBox.Text) != -1)
                {
                    this.listBox.Items.Add(dataSource.Rows[idx][columnName].ToString());
                }
            }
            if (this.listBox.Items.Count==0)
            {
                this.Height = this.textBox.Height;
                this.listBox.Visible = false;
                this.SendToBack();
            }
            else
            {
                if (this.listBox.Items.Count == 1)
                {
                    this.Height = this.textBox.Height + 20 * this.listBox.Items.Count;
                    this.listBox.Visible = true;
                    this.BringToFront();
                }
                else if (this.listBox.Items.Count < 8)
                {
                    this.Height = this.textBox.Height + 14 * this.listBox.Items.Count;
                    this.listBox.Visible = true;
                    this.BringToFront();
                }
                else
                {
                    this.Height = this.textBox.Height + 108;
                    this.listBox.Visible = true;
                    this.BringToFront();
                }
            }
        }
        private void listBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox.Text = this.listBox.SelectedItem.ToString();
                this.Height = this.textBox.Height;
                this.listBox.Visible = false;
                this.SendToBack();
            }
        }
        private void listBox_DoubleClick(object sender, EventArgs e)
        {
            if (this.listBox.SelectedItem != null)
            {
                this.textBox.Text = this.listBox.SelectedItem.ToString();
                this.Height = this.textBox.Height;
                this.listBox.Visible = false;
                this.SendToBack();
            }
        }
        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.listBox.Items.Count != 0 )
                {
                    this.listBox.Focus();
                    this.listBox.SelectedIndex = 0;
                }
            }
        }

        private void textBox_Leave(object sender, EventArgs e)
        {
            if (this.listBox.Focused == false)
            {
                this.Height = this.textBox.Height;
                this.listBox.Visible = false;
                this.SendToBack();
            }
        }
        private void listBox_Leave(object sender, EventArgs e)
        {
            if (this.textBox.Focused == false)
            {
                this.Height = this.textBox.Height;
                this.listBox.Visible = false;
                this.SendToBack();
            }
        }




    }
}
